﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VatGia
{
   // public delegate void ShowFromCT();
    public partial class FormTimKiem : Form
    {
        DataService dservice = new DataService();
        DataTable dt = new DataTable();
        //public event ShowFromCT ShowFormCT;
        private FormMain1 frm;
        public FormTimKiem(FormMain1 frm)
        {
            this.frm = frm;
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormKetQuaTimKiem_Load(object sender, EventArgs e)
        {
            loadLoaiSP();
        }

        private void btnXemChiTiet_Click(object sender, EventArgs e)
        {
            for (int i = dGV_KetQuaTim.SelectedRows.Count - 1; i >= 0; i--)
            {
                if (dGV_KetQuaTim.SelectedRows[i].Index <= dGV_KetQuaTim.Rows.Count - 1)
                {
                    Public_variable.Masp = dGV_KetQuaTim.SelectedRows[i].Cells[0].Value.ToString();
                }
            }
            frm.frm_ShowFormChiTiet();
        }
        public void loadLoaiSP()
        {
            DataTable dt1 = new DataTable();
            cmbLoaiSP.Items.Clear();
            dservice.Connect();
            cmbLoaiSP.DisplayMember = "TenLoai";
            cmbLoaiSP.ValueMember = "MaLoai";
            dt1 =  dservice.ExcuteProcedureGetTable("sp_getLoaiSP", null, null);
            DataRow row = dt1.NewRow();
            row["TenLoai"] = "All";
            row["MaLoai"] = -1;
            dt1.Rows.Add(row);
            cmbLoaiSP.DataSource = dt1;
            dservice.Disconnect();

            cmbLoaiSP.SelectedValue = -1;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                dGV_KetQuaTim.DataSource = null;
                String sql = "SELECT top 100 MaSP,TenSP,DonGia,DonViTinh,TenGH FROM SanPham WHERE ";
                if (txtTenSP.Text != "")
                {
                    sql += "TenSP like N'%" + txtTenSP.Text + "%'";
                    if (txtTenGH.Text != "")
                    {
                        sql += "and TenGH like N'%" + txtTenGH.Text +"%'";
                    }
                    if (Convert.ToInt16(cmbLoaiSP.SelectedValue.ToString()) != -1)
                    {
                        sql += " and MaLoai = " + cmbLoaiSP.SelectedValue.ToString();
                    }
                }
                else
                {
                    if (txtTenGH.Text != "")
                    {
                        sql += "TenGH like N'%" + txtTenGH.Text +"%'";
                        if (Convert.ToInt16(cmbLoaiSP.SelectedValue.ToString()) != -1)
                        {
                            sql += " and MaLoai = " + cmbLoaiSP.SelectedValue.ToString();
                        }
                    }
                    else
                    {
                        if (Convert.ToInt16(cmbLoaiSP.SelectedValue.ToString()) != -1)
                        {
                            sql += "MaLoai = " + cmbLoaiSP.SelectedValue.ToString();
                        }
                    }
                }
                dservice.Connect();
                dt = dservice.ExcuteQueryDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    dGV_KetQuaTim.DataSource = dt;
                    dGV_KetQuaTim.Columns[0].Width = 120;
                    dGV_KetQuaTim.Columns[1].Width = 200;
                    dGV_KetQuaTim.Columns[2].Width = 150;
                    dGV_KetQuaTim.Columns[3].Width = 100;
                    dGV_KetQuaTim.Columns[4].Width = 250;
                }
                else
                {
                    MessageBox.Show("Không tìm thấy kết quả phù hợp,vui lòng thay đổi tiêu chí tìm kiếm");
                }
                dservice.Disconnect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
